package ru.compscicenter.uno.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Date: 15.10.11
 * Time: 1:53
 */
public class Player {
	private int id;
	private String name;
	private List<Card> cards = Collections.synchronizedList(new ArrayList<Card>());
	private int points = 0;
	private boolean saidUno = false;


	public Player() {
		this("");
	}

	public Player(String name) {
		this(name, 0);
	}

	public Player(String name, int id) {
		this.name = name;
		this.id = id;
	}

	public Player(String name, int id, List<Card> cards) {
		this.name = name;
		this.id = id;
		this.cards = cards;
	}

	public synchronized void sayUno(){
		saidUno = true;
	}

	public synchronized boolean isSaidUno(){
		return saidUno;
	}

	public synchronized int getId() {
		return id;
	}

	public synchronized String getName() {
		return name;
	}

	public synchronized List<Card> getCards() {
		return cards;
	}

	public synchronized void setId(int id) {
		this.id = id;
	}

	public synchronized void setName(String name) {
		if (name == null) {
			throw new IllegalArgumentException("setting null name");
		}
		this.name = name;
	}

	public synchronized void setCards(ArrayList<Card> cards) {
		this.cards = cards;
	}

	public synchronized void takeCard(Card card) {
		if (card == null) {
			throw new IllegalArgumentException("taking null card");
		}
		cards.add(card);
		saidUno = false;
	}

	public synchronized void dropCard(Card card) {
		if (!cards.contains(card)) {
			throw new IllegalArgumentException("dropping not existing card");
		}
		cards.remove(card);
	}

	public synchronized int getCardsCount() {
		return cards.size();
	}

	public synchronized void dropCard(int cardId) {
		for (int i = 0; i < cards.size(); i++) {
			Card card = cards.get(i);
			if (cardId == card.getId()) {
				cards.remove(card);
				return;
			}
		}
		throw new IllegalArgumentException("dropping not existing card");
	}

	public synchronized int getCurrentCardsPoints() {
		int result = 0;
		for (Card card : cards) {
			result += card.getPoints();
		}
		return result;
	}

	public synchronized Card getCard(int id) {
		for (Card card : cards) {
			if (card.getId() == id) {
				return card;
			}
		}
		return null;
	}

	public synchronized int getPoints() {
		return points;
	}

	public synchronized void setPoints(int points) {
		this.points = points;
	}

	public synchronized void addPoints(int points) {
		this.points += points;
	}

	public synchronized boolean isWinner() {
		return cards.isEmpty();
	}

	public synchronized void dropAllCards() {
		cards.clear();
	}

	@Override
	public synchronized String toString() {
		return "Player{" +
				"id = " + id +
				",\tname = '" + name + '\'' +
				",\tcards = '" + cards.size() + '\'' +
				'}';
	}

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Player player = (Player) o;

        if (id != player.id) return false;
        if (points != player.points) return false;
        if (saidUno != player.saidUno) return false;
        if (cards != null ? !cards.equals(player.cards) : player.cards != null) return false;
        if (name != null ? !name.equals(player.name) : player.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (cards != null ? cards.hashCode() : 0);
        result = 31 * result + points;
        result = 31 * result + (saidUno ? 1 : 0);
        return result;
    }
}
